function init(){
	var storedValue = air.EncryptedLocalStore.getItem("userappkey");
	if(storedValue != ''){
		verifyid(storedValue);
		getRecentPosts();
	};
};

function verifyid(apikey){
	new Ajax.Request('http://api.ping.fm/v1/user.validate',
	{
		method:'post',
		parameters: {api_key: '9b09a007a957832c31e6608300d16940', user_app_key: apikey},
		onSuccess: function(transport){
			var parser = new DOMImplementation();
			var domDoc = parser.loadXML(transport.responseText);
			var docRoot = domDoc.getDocumentElement();
			if(docRoot.getAttribute("status") == "OK"){
				alert('Checking ID');
				validid();
			} else {	
				document.getElementById('api_key_container').innerHTML = '<p>Sorry, I couldn\'t verify your identity</p>' + document.getElementById('apikey').innerHTML;
			}
		},
		onFailure: function(){ alert('Something went wrong...') }
	});
}

function validid(){
	document.valid_users_only.type_of_request.disabled=false;
	document.valid_users_only.ping_update.disabled=false;
	document.valid_users_only.ping_it_button.disabled=false;
	document.getElementById('api_key_container').innerHTML = '<p>Thank you for validating yourself.</p>' + document.getElementById('apikey').innerHTML;
};

function submit_a_ping(type, message){
	var apikey = air.EncryptedLocalStore.getItem("userappkey");
	alert('made it to submit_a_ping');
	switch(type) {
		case 0:
			type = 'default';
			break;
		case 1:
			type = 'status';
			break;
		case 2:
			type = 'microblog';
			break;
		case 3:
			type = 'blog';
			break;
	}
	new Ajax.Request('http://api.ping.fm/v1/user.post',
	{
		method:'post',
		parameters: {api_key: '9b09a007a957832c31e6608300d16940', user_app_key: apikey, post_method: type, body: message, debug: 1},
		onSuccess: function(transport){
			alert(transport.responseText);
			var parser = new DOMImplementation();
			var domDoc = parser.loadXML(transport.responseText);
			var docRoot = domDoc.getDocumentElement();
			if(docRoot.getAttribute("status") == "OK"){
				alert('It actually would\'ve posted something');
			} else {	
				alert('Bogus, it didn\'t work.');
			}
		},
		onFailure: function(){ alert('Something went wrong...') }
	});	
	//air.trace(Form.serializeElements($('valid_users_only')));
};

function encryptappkey(key){
		var bytes = new air.ByteArray();
		bytes.writeUTFBytes(key);
		air.EncryptedLocalStore.setItem("userappkey", bytes);
}

function getRecentPosts(){
	var apikey = air.EncryptedLocalStore.getItem("userappkey");
	new Ajax.Request('http://api.ping.fm/v1/user.latest',
	{
		method:'post',
		parameters: {api_key: '9b09a007a957832c31e6608300d16940', user_app_key: apikey, order:'DESC'},
		onSuccess: function(transport){
			var parser = new DOMImplementation();
			var domDoc = parser.loadXML(transport.responseText);
			var docRoot = domDoc.getDocumentElement();
			if(docRoot.getAttribute("status") == "OK"){
				alert('Got Recent Stuff');
				var recentposttext='';
				var throwaway=0;
				for(throwaway=0;throwaway<=25;throwaway++){
					var servicetaghandle = docRoot.getElementsByTagName("message").item(throwaway);
					var decodedmessage = decode64(docRoot.getElementsByTagName("body").item(throwaway).childNodes.item(0).getXML());
					//alert(servicetaghandle);
					//alert(decode64(bodytaghandle.childNodes.item(0).getXML()));
					document.getElementById('recent_stuff').innerHTML = document.getElementById('recent_stuff').innerHTML + '<b>' + servicetaghandle.getAttributes().item(1).getNodeValue() + '</b>' + '<br />' + decodedmessage + '<br /><br />';
					//recentposttext = recentposttext + servicetaghandle.getAttributes().item(1).getNodeValue() + '<br />' + decode64(bodytaghandle.childNodes.item(0).getXML()) + '<br /><br />';
				};
				//document.getElementById('recent_stuff').innerHTML = 'Junk' + recentposttext;
			} else {	
				document.getElementById('recent_stuff').innerHTML = 'An error occurred.';
			}
		},
		onFailure: function(){ alert('Something went wrong...') }
	});
}